package whathappens;

import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
import dblib.DB;
import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main {

    public static void main(String[] args) {
        // Logger
        try {
            Logger.getLogger("").addHandler(new FileHandler("errorlog.xml"));
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Look and feel
        try {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Initialisation and Properties
        Vars.PropPath = System.getProperty("user.home") + "/.WhatHappens";
        Vars.PropFileName = Vars.PropPath + "/Options.xml";
        File dir = new File(Vars.PropPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        Boolean NeedInstall = false;
        Boolean NeedConnect = false;
        Vars.LoadProperties();

        if (!Vars.prop.containsKey("Username")) {
            NeedConnect = true;
        }
        if (!Vars.prop.containsKey("Userpass")) {
            NeedConnect = true;
        }
        if (!Vars.prop.containsKey("DB_URL")) {
            NeedConnect = true;
        }

        if (NeedConnect) {
            ConnectDialog dlg = new ConnectDialog(null, true);
            dlg.setLocationRelativeTo(null);
            dlg.setVisible(true);
            if (dlg.getResult().equals("False")) {
                Runtime.getRuntime().exit(1);
            }
            if (dlg.getResult().equals("New")) {
                NeedInstall = true;
            }
        }
        // End of Initialisation and Properties

        // Test server
//        DB.init(Vars.prop.getProperty("Username"), Vars.prop.getProperty("Userpass"), Vars.prop.getProperty("DB_URL"));
//        DB db = new DB();
//        if (db.executeNonQuery("SELECT k FROM maintable WHERE k = 0") != 0) {
//            NeedInstall = true;
//        }
//        if (db.executeNonQuery("SELECT k FROM tagnamestable WHERE k = 0") != 0) {
//            NeedInstall = true;
//        }
//        if (db.executeNonQuery("SELECT k FROM tagstable WHERE k = 0") != 0) {
//            NeedInstall = true;
//        }
//        if (db.executeNonQuery("SELECT k FROM userstable WHERE k = 0") != 0) {
//            NeedInstall = true;
//        }
        if (NeedInstall) {
            FirstInstallDialog dlg = new FirstInstallDialog(null, true);
            dlg.setLocationRelativeTo(null);
            dlg.setVisible(true);
            if (dlg.getResult().equals("ExitPressed")) {
                Runtime.getRuntime().exit(1);
            }
            if (dlg.getResult().equals("False")) {
                Runtime.getRuntime().exit(2);
            }
        }

        Vars.LoadPlugins();

        // Main frame
        MainFrame frm = new MainFrame();
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }
}
